package cn.Tedu.review;

import java.util.Objects;

/*本类用于复习Object中的常用方法*/
public class TestObject {
    public static void main(String[] args) {
        Cat c1 = new Cat();
        Cat c2 = new Cat();
        Cat c3 = new Cat("喵喵",3,99.9);
        Cat c4 = new Cat("喵喵",3,99.9);

        System.out.println(c1==c2);
        System.out.println(c3==c4);
        System.out.println(c1==c4);

        System.out.println(c1.equals(c2));
        System.out.println(c3.equals(c4));
        System.out.println(c1.equals(c4));

        System.out.println(c1);
        System.out.println(c3);
        System.out.println(c1.hashCode());
        System.out.println(c2.hashCode());
        System.out.println(c3.hashCode());
        System.out.println(c4.hashCode());

        String s1="abc";
        String s4="abc";
        char[]value={'a','b','c'};
        String s2=new String(value);
        char[]value1={'a','b','c'};
        String s5=new String(value1);
        String s3="aaa";

        System.out.println(s1==s2);//false
        System.out.println(s1==s3);//false
        System.out.println(s1==s4);//ture
        System.out.println(s2==s5);//false

        System.out.println(s1);//abc
        System.out.println(s2);//abc

        System.out.println(s1.equals(s2));//ture
        System.out.println(s1.equals(s3));//false


    }
}
class Cat{
    String name;
    int age;
    double price;
    public Cat(){
        System.out.println("我是无参构造");
    }

    public Cat(String name, int age, double price) {
        this.name = name;
        this.age = age;
        this.price = price;
        System.out.println("我是全参");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Cat cat = (Cat) o;
        return age == cat.age && Double.compare(cat.price, price) == 0 && Objects.equals(name, cat.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, price);
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", price=" + price +
                '}';
    }
}